package com.ck.core.util;

import com.ck.core.constant.NormalStrConstant;
import com.ck.core.constant.SymbolConstant;

import javax.servlet.http.HttpServletRequest;

/**
 * Http工具类
 * @author 55201
 */
public class HttpUtils {
    /**
     * 获取终端ip
     * @param request   HttpServletRequest
     * @return  String(ip地址)
     */
    public static String getRemortIP(HttpServletRequest request) {
        if (request.getHeader(NormalStrConstant.X_FORWARDED_FOR) == null) {
            return request.getRemoteAddr();
        }

        String headerIP = request.getHeader(NormalStrConstant.X_FORWARDED_FOR);
        // 实际上传的ip在引入DDOS之后，会记录调用链的多个ip，且用逗号分隔，这里取第一个。
        return headerIP.split(SymbolConstant.COMMA_EN)[0];
    }

}
